package SwapSort;

import java.util.Arrays;

public class bubbleSort {
    public static void main(String[] args) {
        int[] a = {45,67,12,3,78,90};
        BubbleSort(a);
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
    }
    //时间复杂度O(n^2)
    //空间复杂度O(1)
    //稳定性:稳定
    private static void BubbleSort(int[] a) {
        boolean flag = true;
        for (int i = 0; i < a.length-1; i++) {
            for (int j = 0; j < a.length-1-i; j++) {
                if(a[j] > a[j+1]){
                    swap(a,j,j+1);
                    flag = false;
                }
            }
            if(flag){
                break;
            }
        }
    }

    private static void swap(int[] a, int j, int i) {
        int tmp = a[j];
        a[j] = a[i];
        a[i] = tmp;
    }
}
